package com.netrust.quarz.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.netrust.quarz.common.NetrustException;
import com.netrust.quarz.common.enums.OperatorEnums;
import com.netrust.quarz.dao.CpuDao;
import com.netrust.quarz.dao.DateReadRecordDao;
import com.netrust.quarz.dao.MemoryDao;
import com.netrust.quarz.dao.ServerDao;
import com.netrust.quarz.dao.StorageDao;
import com.netrust.quarz.entity.Memory;
import com.netrust.quarz.entity.Server;
import com.netrust.quarz.entity.Storage;
import com.netrust.quarz.entity.VCpu;
import com.netrust.quarz.hw.CommonHttp;
import com.netrust.quarz.hw.token.OcUtils;
import com.netrust.quarz.task.utils.StateConverUtils;
import com.netrust.quarz.utils.ProdUtils;

/**
 * 定时生成所需要的源数据
 * @author visitor
 *
 */
@Component  
public class BulidStateService{
	private static final Logger log = LoggerFactory.getLogger(BulidStateService.class);
	
	@Autowired
	CpuDao cpuDao;
	@Autowired
	MemoryDao memoryDao;
	@Autowired
	StorageDao storageDao;	
	@Autowired
	ServerDao serverDao;
	
	@Autowired
	DateReadRecordDao dateReadRecordDao;
	@Autowired
	OcUtils oc;
	


	public String http_run(String op) throws Exception {
		OperatorEnums oe=ProdUtils.op2oe(op);
		
		return this.findStr(oe);
	}
	
	public List<String> http_run_server(String op) throws Exception {
		OperatorEnums oe=ProdUtils.op2oe(op);
		
		oc.setMo(oe.getName());
		
		return new CommonHttp().getServer(oc);
	}
	
	
	@Transactional(isolation=Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	public void run() throws Exception {
		NetrustException exception=null;
		
		List<VCpu> cpuList=Lists.newArrayList();
		List<Memory> memList=Lists.newArrayList();
		List<Storage> stoList=Lists.newArrayList();
		List<Server> servers=serverDao.findServersOnRunning();
		
		try {
			gatherList(cpuList, memList, stoList, servers, OperatorEnums.Operator_dx_zwww);			
		} catch (Exception e) {
			log.error("获取 {} 的实时监控数据异常 {}",OperatorEnums.Operator_dx_zwww.getName(),ExceptionUtils.getStackTrace(e));
			exception=new NetrustException("电信政务外网实时监控数据获取异常", e);
		}
		try {
			gatherList(cpuList, memList, stoList, servers, OperatorEnums.Operator_dx_hlw);
		} catch (Exception e) {
			log.error("获取 {} 的实时监控数据异常 {}",OperatorEnums.Operator_dx_hlw.getName(),ExceptionUtils.getStackTrace(e));
			exception=new NetrustException("电信互联网实时监控数据获取异常", e);
		}
		
		
		try {
			gatherList(cpuList, memList, stoList, servers, OperatorEnums.Operator_yd_zwww);
		} catch (Exception e) {
			log.error("获取 {} 的实时监控数据异常 {}",OperatorEnums.Operator_yd_zwww.getName(),ExceptionUtils.getStackTrace(e));
			exception=new NetrustException("移动政务外网实时监控数据获取异常", e);
		}
		
		try {
			gatherList(cpuList, memList, stoList, servers, OperatorEnums.Operator_yd_hlw);
		} catch (Exception e) {
			log.error("获取 {} 的实时监控数据异常 {}",OperatorEnums.Operator_yd_hlw.getName(),ExceptionUtils.getStackTrace(e));
			exception=new NetrustException("移动互联网实时监控数据获取异常", e);
		}
		
		
		if (CollectionUtils.isNotEmpty(cpuList)) {
			cpuDao.save(cpuList);
		}
		if (CollectionUtils.isNotEmpty(memList)) {
			memoryDao.save(memList);	
		}
		if (CollectionUtils.isNotEmpty(stoList)) {
			storageDao.save(stoList);	
		}
		
		if (exception!=null) {
			throw exception;
		}
	}
	
	
	private void gatherList(List<VCpu> cpuList,List<Memory> memList,List<Storage> stoList,List<Server> servers,OperatorEnums oe) throws Exception{
		StateConverUtils scu=new StateConverUtils();
		scu.convert(servers, this.findStr(oe));
		if (CollectionUtils.isNotEmpty(scu.getCpuList())) {
			cpuList.addAll(scu.getCpuList());			
		}
		if (CollectionUtils.isNotEmpty(scu.getMemList())) {
			memList.addAll(scu.getMemList());			
		}
		if (CollectionUtils.isNotEmpty(scu.getStoList())) {
			stoList.addAll(scu.getStoList());			
		}
	}
	
	
	
	private String findStr(OperatorEnums oe) throws Exception{
		CommonHttp ch=new CommonHttp();
		List<Object> servers=serverDao.findServerObjectId(oe.getName());
		//这里需要转化为String的Object
		List<Object> strObject=Lists.newArrayList();
		for (Object obj : servers) {
			if(obj!=null){
				strObject.add(String.valueOf(obj));
			}
		}
		String objIds=new JSONArray(strObject).toString();
		oc.setMo(oe.getName());
		String str=ch.getServerRealTime(oc, objIds);
		
		return str;
	}

	
	
}
